using System.Linq;
using System.Text;
using SQLite;
using System.Threading.Tasks;

#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#else
using NUnit.Framework;
#endif

using System.IO;

namespace SQLite.Tests
{
	[TestFixture]
	public class OpenTest
	{
		const string UnicodeText = "\u01F427 \u221E";

		[Test]
		public void UnicodePaths()
		{
			var path = Path.GetTempFileName () + UnicodeText;

			using (var db = new SQLiteConnection (path, true)) {
				db.CreateTable<OrderLine> ();
			}

			Assert.IsTrue(new FileInfo(path).Length > 0, path);
		}

		[Test]
		public void UnicodePathsAsync()
		{
			var path = Path.GetTempFileName () + UnicodeText;
			
			var db = new SQLiteAsyncConnection (path, true);
			db.CreateTableAsync<OrderLine> ().Wait ();

			Assert.IsTrue (new FileInfo (path).Length > 0, path);
		}

		[Test]
		public void OpenTemporaryOnDisk()
		{
			var path = string.Empty;
			
			Assert.DoesNotThrow (() => {
				using (var db = new SQLiteConnection (path, true))
				{
					db.CreateTable<OrderLine> ();
				}
			});
		}

		[Test]
		public async Task AsyncWithWalCloses()
		{
			var path = Path.GetTempFileName ();

			var db = new SQLiteAsyncConnection (path);
			await db.CreateTableAsync<OrderLine> ().ConfigureAwait (false);
			await db.InsertAsync (new OrderLine { });
			var lines = await db.Table<OrderLine> ().ToListAsync ();

			Assert.AreEqual (1, lines.Count);

			await db.CloseAsync ();

			File.Delete (path);
		}

		[Test]
		public async Task AsyncWithNoActionsCloses ()
		{
			var path = Path.GetTempFileName ();

			var db = new SQLiteAsyncConnection (path);
			await db.CloseAsync ().ConfigureAwait (false);
		}

	}
}
